<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #0f9fff">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/login.png" alt="" style="width: 100% ;height: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎登录口腔诊所管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
import ValidCode from "@/views/ValidCode.vue";
import axios from "axios";

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    return {
      user: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  created() {

  },
  methods: {
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          axios.post('http://localhost:8080/loginController/login', this.user)
              .then(response => {
                  if(response.data.code==200) {
                      localStorage.setItem('userData', JSON.stringify(response.data.data));
                      this.$message.success("登录成功");
                      this.$router.push("/home");
                  }
                  else {
                      this.$message.error("登录失败");
                  }
              })
              .catch(error => {
                  console.error('Error adding new item:', error);
                  this.$message.error("登录失败");
              });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>